package com.china08.yxyapi.api.home.activity.healthcompetition.huodongtongji;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudent;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentRepository;

/**
 * 健康知识竞赛 学生排名
 * 
 * @author zd.Jin
 *
 *
 */
@CrossOrigin
@RestController
public class HealthCompetionTongjiByStuPageController extends YxyApiBaseController {
	@Autowired
	private YxyCompetitionStudentRepository yxyCompetitionStudentRepository;

	@RequestMapping(value = "/api_yw/home/activity/healthcompetition/huodongtongji/page/studenttongji", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<RespModel> process(HttpServletRequest request, //
			HttpServletResponse response, //
			@RequestBody(required = false) ReqModel reqModel) {
		Pageable pageable = reqModel.getPageable(Direction.DESC, "score");
		String school = reqModel.schoolId;
		Integer answerornot = reqModel.answerornot;
		String gradeName = reqModel.gradeName;
		String classId = reqModel.classId;
		Page<YxyCompetitionStudent> findAll = null;
		if (answerornot == 2) {// 当是否作答选择全部时
			if ("all".equals(gradeName)) {// 当年级选择全部时
				findAll = yxyCompetitionStudentRepository.findBySchoolId(school, pageable);
			} else {// 当年级选择时
				if ("all".equals(classId)) {// 当班级选择全部时
					findAll = yxyCompetitionStudentRepository.findBySchoolIdAndGrade(school, gradeName, pageable);
				} else {// 当班级选择时
					findAll = yxyCompetitionStudentRepository.findBySchoolIdAndGradeAndClassId(school, gradeName,
							classId, pageable);
				}
			}
		} else if (answerornot == 0) {// 当选择完成作答时
			if ("all".equals(gradeName)) {// 当年级选择全部时
				findAll = yxyCompetitionStudentRepository.findBySchoolIdAndStatus(school, 2, pageable);
			} else {// 当年级选择时
				if ("all".equals(classId)) {// 当班级选择全部时
					findAll = yxyCompetitionStudentRepository.findBySchoolIdAndStatusAndGrade(school, 2, gradeName,
							pageable);
				} else {// 当班级选择时
					findAll = yxyCompetitionStudentRepository.findBySchoolIdAndStatusAndGradeAndClassId(school, 2,
							gradeName, classId, pageable);
				}
			}
		} else {// 当选择未作答时
			if ("all".equals(gradeName)) {// 当年级选择全部时
				findAll = yxyCompetitionStudentRepository.findBySchoolIdAndStatusNot(school, 2, pageable);
			} else {// 当年级选择时
				if ("all".equals(classId)) {// 当班级选择全部时
					findAll = yxyCompetitionStudentRepository.findBySchoolIdAndStatusNotAndGrade(school, 2, gradeName,
							pageable);
				} else {// 当班级选择时
					findAll = yxyCompetitionStudentRepository.findBySchoolIdAndStatusNotAndGradeAndClassId(school, 2,
							gradeName, classId, pageable);
				}
			}
		}

		RespModel respModel = null;
		List<RespModel> list = new ArrayList<RespModel>();
		for (YxyCompetitionStudent yxyCompetitionStudent : findAll) {
			respModel = new RespModel();
			respModel.name = yxyCompetitionStudent.getStudentNick();
			respModel.grade = yxyCompetitionStudent.getGrade();
			respModel.clazz = yxyCompetitionStudent.getClassName();
			respModel.score = yxyCompetitionStudent.getScore();
			list.add(respModel);
		}
		Page<RespModel> page = new PageImpl<>(list, pageable, findAll.getTotalElements());
		return page;
	}

	protected static class ReqModel extends YxyApiPageReqModel {
		// 学校id
		public String schoolId;
		// 是否完成作答(0是1否2全部)
		public Integer answerornot;
		// 所在年级名称(全部传"all")
		public String gradeName;
		// 所在班级id(全部传"all")
		public String classId;

		@Override
		public void autoCheck() {
			Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "请填写学校ID");
			Assert.notNull(StringUtils.defaultIfBlank(answerornot + "", null), "请传是否作答");
			Assert.notNull(StringUtils.defaultIfBlank(gradeName, null), "请填写年级名称");
			Assert.notNull(StringUtils.defaultIfBlank(classId, null), "请填写班级ID");
		}

	}

	protected static class RespModel extends YxyApiRespModel {
		// 姓名
		public String name;
		// 年级
		public String grade;
		// 班级
		public String clazz;
		// 分数
		public Integer score;
	}
}
